// pages/reviewRecord/todayreview/todayreview.js
const app = getApp();
let tool = require('../../../utils/tool.js');
const httpUtils = require('../../../utils/httpUtils.js');
const versionutil = require('../../../utils/versionutil.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    selectedIndex: -1, //当前选中的条目
    reviewTask: [], //复习任务
    has_access: 0, //0默认无评价，1为有评价内容
    appraiseContent:"", //评价内容
    date: '2020-08-12', //日期
    reviewTime:"19:00", //复习推送时间
    reviewStatus: "1", //复习状态 0 复习完成可查看错词；1有复习任务；2 今日无复习任务；3未到复习时间
    task_id: '',//任务id
    source:0, //0从今日复习进入，1从复习记录进入
    finish_num:0, //已完成数
    not_finish_num:0,//未完成数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let date = options.date;
    console.log("todyreview界面接收到的date=" + date)
    let source=options.source;
    that.setData({
      date:date,
      source:source
     })

    if(source == 0){
       that.setData({
        reviewStatus: options.reviewStatus, //当天的复习状态
        reviewTime: options.push_time,//推送时间
       })
    }

  },

  //获取复习任务
  getReviewTask() {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getReviewTask({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      date: that.data.date
    }).then((res) => {
      let has_access=0;
      if(!res.data.appraise){
        has_access=0;
      }else{
        has_access=1;
      }
      let task=res.data;
      for(var i=0;i<task.review.relength;i++){
        task.review[i].isOpen=false;
      }
      task.review[0].isOpen=true;
      that.setData({
        reviewTask: task,
        task_id: res.data.id,
        finish_num:res.data.finish_num,
        not_finish_num:res.data.not_finish_num,
        has_access:has_access,
        appraiseContent:res.data.appraise,
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.getReviewTask();
  },

  // 点击条目展开隐藏
  isOpen(e) {
    let that = this;
    let reviewTask=that.data.reviewTask;
    let index = e.currentTarget.dataset.index;
    console.log("index=" + index)
    for(var i=0; i<reviewTask.review.length; i++){
        if(i == index){
          reviewTask.review[i].isOpen=!reviewTask.review[i].isOpen;
        }else{
          reviewTask.review[i].isOpen=false; 
        }   
    }

    that.setData({
      reviewTask:reviewTask
    })
  },

  //点击条目跳转对应界面去复习
  gotoreview(e) {
    let that = this;
    let task_id = that.data.task_id;
    console.log("taskid=" + task_id);

    let coursetype = e.currentTarget.dataset.coursetype;
    let sort = e.currentTarget.dataset.sort;
    let can_submit=e.currentTarget.dataset.cansubmit;
    let status=e.currentTarget.dataset.status;
    let question_id = e.currentTarget.dataset.cid;
    console.log("coursetype=" + coursetype + ",sort=" + sort+",can_submit="+can_submit);
   
      //获取录音权限（回头放到点击开始复习入口判断）
    that.getRecordAuth();
    switch (sort) {
      case 0:
        if(can_submit ==0){
          wx.navigateTo({
            url: '../lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id + "&can_submit=0" + "&date=" + that.data.date + "&type=0",
          })
        }else{
          wx.navigateTo({
            url: '/pages/testReport/testReport?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }
  
        break;
      case 1:
        //词义闯关
        if(can_submit ==0){
          //status 为2已复习时，或者不能提交时，  查看错词
          wx.navigateTo({
            url: '../lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id + "&can_submit=0" + "&date=" + that.data.date + "&type=0",
          })
        }else {
          wx.navigateTo({
            url: '/pages/reviewRecord/worldMean/worldMean?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }
        break;
      case 2:
        //对对碰
        if(can_submit ==0){
          //status 为2已复习时，跳转查看错词界面
          wx.navigateTo({
            url: '../lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id + "&can_submit=0" + "&date=" + that.data.date + "&type=0",
          })
        }else {
          wx.navigateTo({
            url: '/pages/reviewRecord/takesTwo/takesTwo?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }

        break;
      case 3:
        //听音识义
        if(can_submit ==0){
          //status 为2已复习时，或者不能提交时，跳转查看错词界面
          wx.navigateTo({
            url: '../lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id + "&can_submit=0" + "&date=" + that.data.date + "&type=0",
          })
        }else {
          wx.navigateTo({
            url: '/pages/reviewRecord/listenWorld/listenWorld?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }
        break;
      case 4:
        //组合拼
        if(can_submit ==0){
          // wx.navigateTo({
          //   url: '/pages/reviewRecord/lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id,
          // })

          wx.navigateTo({
            url: '../lwordWrong/lwordWrong?sort=' + sort + "&task_id=" + task_id + "&can_submit=0" + "&date=" + that.data.date + "&type=0",
          })
        }else{
          wx.navigateTo({
            url: '/pages/reviewRecord/spellword/spellWord?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }
        break;
      case 5:
        //单词 读一遍
        if(that.data.source==0){
          wx.navigateTo({
            url: '/pages/reviewRecord/wordReadWord/wordReadWord?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&status="+status,
          })
        }else{
          wx.navigateTo({
            url: '/pages/reviewRecord/wordwork/wordwork?sort=' + sort + "&task_id=" + task_id+"&can_submit="+can_submit+"&status="+status,
          })
        }
    
        break;
      case 6:
        //单词写一遍
        wx.navigateTo({
          url: '/pages/reviewRecord/wordwork/wordwork?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit+"&status="+status,
        })
        break;
  
      case 7:
        //阅读做题拍照  
        // wx.navigateTo({
        //   url: '/pages/reviewRecord/readlist/readlist?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit+"&status="+status,
        // })
        wx.navigateTo({
          url: '/pages/reviewRecord/readwork/readwork?sort=' + sort + "&calendar_id=" + that.data.task_id+"&can_submit="+can_submit+"&status="+status+"&question_id="+question_id,
        })
        break;
      case 8:
        //阅读 读一遍
        wx.navigateTo({
          url: '/pages/reviewRecord/readwork/readwork?sort=' + sort + "&calendar_id=" + that.data.task_id+"&can_submit="+can_submit+"&status="+status+"&question_id="+question_id,
        })
        break;
      case 9:
        //语法 语法填空
        if(can_submit ==0){
          //stus 为2已复习时，跳转查看错句界面
          wx.navigateTo({
            url: '/pages/studyRecord/grammar/grammar?date=' + that.data.date+"&sort="+sort+"&task_id=" + that.data.task_id+"&can_submit="+can_submit,
          })
        }else {
          // 可以提交复习时，或者未复习 跳转语法填空界面
          wx.navigateTo({
            url: '/pages/studyRecord/grammarReview/grammarReview?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit+"&date=" + that.data.date,
          })
        }  
        break;
      case 10:
        //语法 写一遍
        wx.navigateTo({
          url: '/pages/reviewRecord/grammarwork/grammarwork?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit,
        })
        break;
      case 11:
        //语法 读一遍
        wx.navigateTo({
          url: '/pages/reviewRecord/grammarwork/grammarwork?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit,
        })
        break;
      case 13:
        //写作 写一遍
        wx.navigateTo({
          url: '/pages/studyRecord/writing/writing?sort=' + sort + "&task_id=" + that.data.task_id+"&can_submit="+can_submit,
        })
        break;
      case 14:
        //写作 读一遍
        wx.navigateTo({
          url: '/pages/reviewRecord/essay/essay?sort=' + sort + "&task_id=" + that.data.task_id+'&source=1'+"&can_submit="+can_submit+"&status="+status,
        })
        break;

      default:
        break;

    }

  },

    // 获取录音权限询问
    getRecordAuth: function() {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.record']) {
            wx.authorize({
              scope: 'scope.record',
              success() {
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                console.debug("授权成功了")
              },
              fail() {
                console.error("授权失败了");
                wx.showToast({
                  title: '请您授权录音权限',
                });
                if (versionutil.compageNowVersion('2.0.8') >= 0) {
                  wx.navigateTo({
                    url: '../authorize/record/record',
                  })
                } else {
                  wx.openSetting({
  
                  });
                }
              }
            })
          } else {}
        },
        fail(res) {
          console.log("fail")
          console.log(res)
        }
      })
    },
    
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})